package ar.edu.unq.wannaplay.panels.client;

import ar.edu.unq.wannaplay.exceptions.BlankStringException;
import ar.edu.unq.wannaplay.exceptions.EmailSyntaxException;
import ar.edu.unq.wannaplay.pages.client.ClientsViewPage;
import ar.edu.unq.wannaplay.panels.AbstractBasicEditPanel;
import ar.edu.unq.wannaplay.panels.FeedBackPanel;
import ar.edu.unq.wannaplay.services.ClientService;
import ar.edu.unq.wannaplay.utils.ClientEditer;

public class EditClientPanel extends AbstractBasicEditPanel<ClientEditer, ClientService> {

    private static final long serialVersionUID = 1L;

    public EditClientPanel(final String id, final ClientEditer modelObject, final ClientService service,
            final FeedBackPanel feedBackPanel) {
        super(id, modelObject, service, feedBackPanel);
    }

    @Override
    protected String[] getFieldsStrings() {
        return new String[] { "name", "email", "address", "town" };
    }

    @Override
    protected void acceptAction() {
        ClientEditer bind = this.getModelObject();
        try {
            this.errorWrapedAction(bind);
        } catch (BlankStringException e) {
            this.getFeedBackPanel().changeMessage(this.getLocalizer().getString("client.error.blankString", this));
        } catch (EmailSyntaxException e) {
            this.getFeedBackPanel().changeMessage(this.getLocalizer().getString("client.error.emailSyntax", this));
        }

    }

    protected void errorWrapedAction(final ClientEditer bind) throws BlankStringException, EmailSyntaxException {
        this.getService()
                .updateClient(bind.getId(), bind.getName(), bind.getEmail(), bind.getAddress(), bind.getTown());
        this.setResponsePage(ClientsViewPage.class);
    }

    @Override
    protected void addComponents() {
        // no specific components
    }

}
